import React, { Component } from 'react'

export default class ErrorBoundary extends Component {
  constructor() {
    super()
    this.state = {
      hasError: false
    }
  }
  componentDidCatch() {
    // 组件已经报错了
    this.setState({
      hasError: true
    })
  }
  render() {
    return (
      <div>
        {/* this.props.children是这个组件的子组件 */}
        {this.state.hasError?(<div>报错了...</div>): this.props.children}
      </div>
    )
  }
}
